﻿using Super.Admin.Entities;
using Super.Admin.Models.Department;

namespace Super.Admin.Mappers
{
    /// <summary>
    /// DepartmentMapper 类用于在 DepartmentEntity 和 DepartmentModel 之间进行转换。
    /// </summary>
    public static class DepartmentMapper
    {
        /// <summary>
        /// 将 DepartmentEntity 转换为 DepartmentBaseModel。
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static DepartmentBaseModel ToBaseModel(this DepartmentEntity entity)
        {
            if (entity == null) return null;
            return new DepartmentBaseModel
            {
                Id = entity.Id,
                Name = entity.Name,
                ParentId = entity.ParentId,
                IsActive = entity.IsActive,
                CreateTime = entity.CreateTime,
                UpdateTime = entity.UpdateTime,
                Remark = entity.Remark,
                PersonInChargeId = entity.PersonInChargeId
            };
        }
        /// <summary>
        /// 将 DepartmentEntity 转换为 DepartmentDetailModel。
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static DepartmentDetailModel ToDetailModel(this DepartmentEntity entity)
        {
            if (entity == null) return null;
            return new DepartmentDetailModel
            {
                Id = entity.Id,
                Name = entity.Name,
                ParentId = entity.ParentId,
                IsActive = entity.IsActive,
                CreateTime = entity.CreateTime,
                UpdateTime = entity.UpdateTime,
                Remark = entity.Remark,
                PersonInChargeId = entity.PersonInChargeId,
                PersonInChargeModel = entity.PersonInChargeModel?.ToBaseModel(),
                Users = (List<Models.User.UserBaseModel>)(entity.Users?.ToBaseModels()),
                PersonInChargeName = entity.PersonInChargeModel?.UserName,
                IsActiveTag = entity.IsActive == 0 ? new AntdUI.CellTag[] { new AntdUI.CellTag("启用", AntdUI.TTypeMini.Success) } : entity.IsActive == 1 ? new AntdUI.CellTag[] { new AntdUI.CellTag("停用", AntdUI.TTypeMini.Default) } : new AntdUI.CellTag[] { new AntdUI.CellTag("未知", AntdUI.TTypeMini.Default) }
            };
        }
        /// <summary>
        /// 将 DepartmentBaseModel 转换为 DepartmentEntity。
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static DepartmentEntity ToEntity(this DepartmentBaseModel model)
        {
            if (model == null) return null;
            return new DepartmentEntity
            {
                Id = model.Id,
                Name = model.Name,
                ParentId = model.ParentId,
                IsActive = model.IsActive,
                CreateTime = model.CreateTime,
                UpdateTime = model.UpdateTime,
                Remark = model.Remark,
                PersonInChargeId = model.PersonInChargeId
            };
        }
        /// <summary>
        /// 将 DepartmentDetailModel 转换为 DepartmentEntity。
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static DepartmentEntity ToEntity(this DepartmentDetailModel model)
        {
            if (model == null) return null;
            return new DepartmentEntity
            {
                Id = model.Id,
                Name = model.Name,
                ParentId = model.ParentId,
                IsActive = model.IsActive,
                CreateTime = model.CreateTime,
                UpdateTime = model.UpdateTime,
                Remark = model.Remark,
                PersonInChargeId = model.PersonInChargeId
            };
        }
        /// <summary>
        /// 将 DepartmentEntity 列表转换为 DepartmentBaseModel 列表。
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IEnumerable<DepartmentBaseModel> ToBaseModels(this IEnumerable<DepartmentEntity> entities)
        {
            return entities?.Select(e => e.ToBaseModel()).ToList() ?? new List<DepartmentBaseModel>();
        }
        /// <summary>
        /// 将 DepartmentEntity 列表转换为 DepartmentDetailModel 列表。
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IEnumerable<DepartmentDetailModel> ToDetailModels(this IEnumerable<DepartmentEntity> entities)
        {
            return entities?.Select(e => e.ToDetailModel()).ToList() ?? new List<DepartmentDetailModel>();
        }
        /// <summary>
        /// 将 DepartmentBaseModel 列表转换为 DepartmentEntity 列表。
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public static IEnumerable<DepartmentEntity> ToEntities(this IEnumerable<DepartmentBaseModel> models)
        {
            return models?.Select(m => m.ToEntity()).ToList() ?? new List<DepartmentEntity>();
        }
    }
}
